<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="快递单号" prop="barcode">
        <el-input
          v-model="queryParams.barcode"
          placeholder="请输入快递单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--     <el-form-item label="视频类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择视频类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
<!--     <el-form-item label="ip" prop="ip" >
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入ip"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="视频创建时间" label-width="120px">
        <el-date-picker
          v-model="daterangeVideoCreateTime"
          size="small"
          style="width: 360px"
          :default-time="['00:00:00']"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="主机名" prop="hostName" >
         <el-input
           v-model="queryParams.hostName"
           placeholder="请输入主机名"
           clearable
           size="small"
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>
      <el-form-item label="上报时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          size="small"
          style="width: 360px"
          :default-time="['00:00:00']"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['videoMonitor:video:add']"
        >详情</el-button>
      </el-col> -->
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['videoMonitor:video:edit']"
        >修改</el-button>
      </el-col> -->
     <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['videoMonitor:video:remove']"
        >删除</el-button>
      </el-col> -->
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['videoMonitor:video:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="left" width="100" prop="id" />
      <el-table-column label="快递单号" align="left" prop="barcode" width="150" >

<!--        <template slot-scope="scope">
          <el-button type="text" size="small">
            <a :href="'' + " @click="@click="handleDetail(scope.row)" >barcode</a>
          </el-button>
        </template> -->
      </el-table-column>
      <el-table-column label="视频创建时间" align="left" prop="videoCreateTime" />

<!--      <el-table-column label="视频类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="视频相对地址" align="center" prop="videoPath" /> -->
      <el-table-column label="上报时间" align="left" prop="createTime" />
      <el-table-column label="主机名" align="left" prop="hostName" />
      <el-table-column label="操作" align="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['videoMonitor:video:edit']"
          >详情</el-button>
<!--         <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="downloadVideo(scope.row)"
            v-hasPermi="['videoMonitor:video:edit']"
          >下载</el-button> -->
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['videoMonitor:video:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['videoMonitor:video:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


<!-- 添加或修改监控视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1064px" append-to-body style="padding-left: 0px;">
        <video id="playVideo" width="1024" height="640" controls  :src="videoUrl" autoplay  />
    </el-dialog>

    <!-- 添加或修改监控视频对话框 -->
   <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="快递单号" prop="barcode">
          <el-input v-model="form.barcode" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="视频类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择视频类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="视频相对地址" prop="videoPath">
          <el-input v-model="form.videoPath" placeholder="请输入视频相对地址" />
        </el-form-item>
        <el-form-item label="是否删除 0未删除 1已删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除 0未删除 1已删除" />
        </el-form-item>
        <el-form-item label="创建人" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateName">
          <el-input v-model="form.updateName" placeholder="请输入更新人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listVideo, getVideo, delVideo, addVideo, updateVideo, exportVideo } from "@/api/videoMonitor/video";
import request from '@/utils/request';
// import axios from 'axios'
// const service = axios.create({
//   baseURL: '',
//   headers: {
//     // 'Content-Type': 'text/plain;charset=UTF-8'
//   },
//   responseType:Blob
// })
export default {
  name: "Video",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监控视频表格数据
      videoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      videoUrl: "",
      // 视频类型字典
      typeOptions: [],
      // 创建时间时间范围
      daterangeVideoCreateTime: [],
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 更新时间时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        barcode: null,
        type: null,
        videoPath: null,
        hostName: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("video_type").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** 查询监控视频列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeVideoCreateTime && '' != this.daterangeVideoCreateTime) {
        this.queryParams.params["beginVideoCreateTime"] = this.daterangeVideoCreateTime[0];
        this.queryParams.params["endVideoCreateTime"] = this.daterangeVideoCreateTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listVideo(this.queryParams).then(response => {
        this.videoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 视频类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        barcode: null,
        type: null,
        videoPath: null,
        isDelete: null,
        createBy: null,
        createName: null,
        createTime: null,
        updateBy: null,
        updateName: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeVideoCreateTime = [];
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "视频详情" ;
    },
    /** 修改按钮操作 */
    handleDetail(row) {

      this.reset();
      const id = row.id || this.ids
      getVideo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.videoUrl = process.env.VUE_APP_VIDEO_PREFIX + response.data.videoPath;
        this.title = "快递单号: " +  response.data.barcode;
      });
      var video = document.getElementById("playVideo");
      video.currentTime = 0; // 设置开始播放的位置为10秒
      video.play();
    },

    downloadVideo(row){
       // service.request({
       //            url: `http://192.168.1.21:8081/api/process/apply/union-apply/applyFor`,
       //            method: 'post',
       //            data: this.formData,
       //          }).then((res) => {
       //            if (res.data.code == 200) {
       //              this.$message.success('操作成功！')
       //            }
       //          })
       //        }


      try {
        console.log(row.videoPath);
        console.log(process.env.VUE_APP_VIDEO_PREFIX);
        const response = request(process.env.VUE_APP_VIDEO_PREFIX + row.videoPath,{responseType: 'blob'});
        const blob = new Blob([response.data])
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = row.barcode + ".mp4";
        link.click();
      } catch (error) {
        console.error('Error fetching the image:', error);
      }

    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVideo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除监控视频编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVideo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有监控视频数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportVideo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
